<template>
  <div>
    <el-dialog title="车辆调拨" :visible="openTb" @close="cancel" width="800px" destroy-on-close append-to-body>
      <el-form ref="elForm" :model="form" :rules="rules" size="medium" label-width="100px">
        <el-form-item label="车牌号" prop="carNo">
          <el-input v-model="form.carNo" placeholder="请输入车牌号" readonly clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="调拨门店" required>
          <div @click="showCallStoreBox=true">
            <el-input v-model="form.callStoreName" placeholder="请选择调入门店" clearable :style="{width: '100%'}" readonly>
              <el-button slot="append" icon="el-icon-search"></el-button>
            </el-input>
          </div>
        </el-form-item>
        <el-form-item label="调拨时间" prop="startTime">
          <el-date-picker type="datetime" v-model="form.startTime" format="yyyy-MM-dd HH:mm:ss"
                          value-format="yyyy-MM-dd HH:mm:ss" :style="{width: '100%'}" placeholder="请选择调拨时间" clearable>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" :maxlength="50"
                    show-word-limit :autosize="{minRows: 2, maxRows: 3}" :style="{width: '100%'}"></el-input>
        </el-form-item>

        <el-form-item v-if="showMsg">
          <div style="color: red">{{msg}}</div>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="handelConfirm" :loading="submitBtnLoading">确定</el-button>
      </div>
    </el-dialog>
    <select-store :show="showCallStoreBox" @success="selectCallStoreInfo" :all="true"></select-store>
  </div>
</template>
<script>
import {
  addSecondment,secondmentPreInspection
} from "@/api/car/secondment";
import selectStore from "@/views/components/selectStore.vue";
export default {
  inheritAttrs: false,
  components: {
    selectStore
  },
  props: {
    carId: {
      type: String,
      default: ''
    },
    carNo: {
      type: String,
      default: ''
    },
    openTb: {
      type: Boolean,
      default: false
    },
  },
  data() {
    return {

      showMsg:false,
      msg:"",

      submitBtnLoading: false,
      showCallStoreBox: false,
      form: {
        carId: undefined,
        carNo: undefined,
        callStore: null,
        callStoreName: null,
        startTime:null,
        remark:null
      },
      rules: {
        carNo: [{
          required: true,
          message: '请输入车牌号',
          trigger: 'blur'
        }],
        // callStore: [{
        //   required: true,
        //   message: '请选择调拨门店',
        //   trigger: 'blue'
        // }],
        startTime:[{
          required: true,
          message: '请选择调拨时间',
          trigger: 'change'
        }],
      },
    }
  },
  created() {
    this.form.carNo=this.carNo
    this.form.carId=this.carId
  },
  mounted() {},
  methods: {
    selectCallStoreInfo(store) {
      this.showCallStoreBox = false;
      if (store) {
        this.form.callStore = store.storeId;
        this.form.callStoreName = store.storeName;
      }
    },
    cancel() {
      this.$emit("close")
    },
    handelConfirm() {
      if (this.form.callStore == null || this.form.callStore == ''){
        this.$message({message:'请选择调拨门店', type:'error'})
        return
      }
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          this.submitBtnLoading = true
          secondmentPreInspection(this.form).then(res => {
            switch (res.data.code){
              case 0:
                addSecondment(this.form).then(response => {
                  this.$modal.msgSuccess("操作成功");
                  this.$emit("close")
                });
                break;
              case 1:
              case 2:
              case 3:
              case 4:
                this.submitBtnLoading = false
                this.showMsg=true;
                this.msg = res.data.msg;
                break;
              case 5:
                this.$confirm(res.data.msg, '提示', {
                  confirmButtonText: '确认',
                  cancelButtonText: '取消',
                  type: 'warning'
                }).then(() => {
                  addSecondment(this.form).then(response => {
                    this.$modal.msgSuccess("操作成功");
                    this.$emit("close")
                  }).finally(err=>{
                    this.submitBtnLoading = false
                  });
                })
                break;
              case 6:
                this.submitBtnLoading = false
                this.showMsg=true;
                this.msg = res.data.msg;
                break;
            }
          })
        }
      })
    },
  }
}

</script>
<style>
</style>
